package app.server.project.main.service.service;

import app.server.project.core.constants.Def;
import app.server.project.main.service.db.dao.RoomDao;
import app.server.project.main.service.db.entity.RoomEntity;
import app.server.project.main.service.dto.room.RoomInfoDto;
import cn.hutool.core.util.IdUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class RoomService {

    private RoomDao roomDao;

    public List<RoomInfoDto> getRoomsByUserId(long userId){
        var data=this.roomDao.getData(userId);
        return data.stream().map(RoomInfoDto::of).toList();
    }

    public boolean delRoom(long userId,long roomId){
        var data=this.roomDao.getData(userId);
        return data.removeIf(v->{
            if (v.getId()==roomId){
                v.setIsDel(Def.True);
                return true;
            }
            return false;
        });
    }

    public void addRoom(long userId,RoomInfoDto dto){
        var data=this.roomDao.getData(userId);
        data.add(RoomEntity.of(IdUtil.getSnowflakeNextId(),userId, dto.getName()));
    }

    public void updateName(long userId,RoomInfoDto dto){
        var data=this.roomDao.getData(userId);
        data.forEach(v->{
            if (v.getId()==dto.getId()){
                v.setName(dto.getName());
            }
        });
    }


}
